const express = require('express')
const router = express.Router()
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')

// 获取自己的意见
router.post("/getUserOp",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.user.email]
    connection.query(`SELECT * FROM opinion_table WHERE uEmail=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！',result)
        }
    })
})

// 添加意见
router.post("/add",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.user.email,req.body.content," "]
    connection.query(`INSERT INTO opinion_table (uEmail,content,recoveText) VALUES (?,?,?)`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 回复意见
router.post("/update",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params =  [req.body.recoveText,1,req.body.id]
    connection.query(`UPDATE opinion_table SET recoveText=?,recoveStatus=? WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 删除
router.post("/delete",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.id]
    connection.query(`DELETE FROM opinion_table WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

module.exports = router